﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TreeControl;

namespace TreeControlTester
{
    public partial class TreeControlTesterForm : Form
    {
        public TreeControlTesterForm()
        {
            InitializeComponent();
            InitTreeControl();
        }

        #region Control Releted Code
        private void InitTreeControl()
        {
            Tree t = new Tree();
            t.Name = "myControl";
            t.Width = 200;
            t.BackColor = Color.Gray;
            t.Location = new Point(290, 10);
            this.Controls.Add(t);

            for (int i = 1; i <= 6; i++)
            {
                TreeItem item = t.AddItem(i.ToString(), i.ToString());
            }

            int height = 0;
            foreach (TreeItem child in t.Items)
            {
                height += child.BoundingRectangle.Height;
            }

            t.Height = height;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InitControl();
        }

        private void InitControl()
        {
            string[] nodes = new string[5] { "node 1 ", "node 2 ", "node 3 ", "node 4 ", "node 5 " };

            MeasurementControl control = new MeasurementControl("treeview");

            control.AddMeasurementItem("treeview", "treeview", MeasurementType.MeasurementGroup, Behavior.AutoSeries);

            foreach (string node in nodes)
            {
                control.AddMeasurementItem(node, node, MeasurementType.ChildMeasurement, Behavior.Normal);
            }

            control.FinalizeCreation();

            control.Size = new Size(260, 30);
            control.Location = new Point(10, 10);
            this.Controls.Add(control);

            control.NodeCollapsed += new EventHandler<NodeClickedEventArgs>(control_NodeCollapsed);
            control.NodeDoubleClicked += new EventHandler<NodeClickedEventArgs>(control_NodeDoubleClicked);
            control.NodeExpanded += new EventHandler<NodeClickedEventArgs>(control_NodeExpanded);
            control.NodeSelected += new EventHandler<NodeClickedEventArgs>(control_NodeSelected);

        }

        void control_NodeSelected(object sender, NodeClickedEventArgs e)
        {
            LogInTextBox("Node Selected " + e.Id);
        }

        void control_NodeExpanded(object sender, NodeClickedEventArgs e)
        {
            LogInTextBox("Node Expanded " + e.Id);
        }

        void control_NodeDoubleClicked(object sender, NodeClickedEventArgs e)
        {
            LogInTextBox("Node DoubleClicked " + e.Id);
        }

        void control_NodeCollapsed(object sender, NodeClickedEventArgs e)
        {
            LogInTextBox("Node Collapsed " + e.Id);
        }

        private void LogInTextBox(string str)
        {
            richTextBox1.AppendText(DateTime.Now.ToLongTimeString() + " :: " + str);
            richTextBox1.AppendText("\r\n");
            //richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.ScrollToCaret();
        }


        #endregion
    }
}
